/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cadProdutoJInternalFrame.java
 *
 * Created on 27/10/2009, 21:03:56
 */
package br.com.fatecpg.zanotti.jframes.consultas;

import br.com.fatecpg.zanotti.acessobanco.conTableCliente;
import br.com.fatecpg.zanotti.acessobanco.conTableVenda;
import br.com.fatecpg.zanotti.acessobanco.dmlConta_Rec;
import br.com.fatecpg.zanotti.acessobanco.dmlPedido;
import br.com.fatecpg.zanotti.acessobanco.dmlPedido_tem_produto;
import br.com.fatecpg.zanotti.jframes.cadastros.cadVendaJInternalFrame;
import br.com.fatecpg.zanotti.modelo.Cliente;
import br.com.fatecpg.zanotti.modelo.Contas_receber;
import br.com.fatecpg.zanotti.modelo.Pedido;

/**
 *
 * @author Luiz Zanotti
 */
public class conVendaJInternalFrame extends javax.swing.JInternalFrame {

    /** Creates new form cadProdutoJInternalFrame */
    public conVendaJInternalFrame() {
        initComponents();
        conVendajTable.getColumnModel().getColumn(1).setMaxWidth(90);
        conVendajTable.getColumnModel().getColumn(1).setMinWidth(90);
        conVendajTable.getColumnModel().getColumn(0).setMaxWidth(300);
        conVendajTable.getColumnModel().getColumn(0).setMinWidth(210);
    }

    public void atualizaTable() {
        conVendajTable.removeAll();
        conTableVenda con = new conTableVenda();
        conVendajTable = con.ConsultaTabelaToda(conVendajTable);
    }
    private static conVendaJInternalFrame p;

    public static conVendaJInternalFrame getInstancia() {
        if (p == null) {
            p = new conVendaJInternalFrame();
        }
        return p;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        conVendajTable = new javax.swing.JTable();
        pesquisarjLabel = new javax.swing.JLabel();
        buscarjTextField = new javax.swing.JTextField();
        nome_cpfjComboBox = new javax.swing.JComboBox();
        buscarjButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        alterarjButton = new javax.swing.JButton();
        excluirjButton = new javax.swing.JButton();
        novojButton = new javax.swing.JButton();
        novojButton1 = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Consulta de Vendas");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        conVendajTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Cliente", "Código"
            }
        ));
        jScrollPane1.setViewportView(conVendajTable);

        pesquisarjLabel.setText("Pesquisar por:");

        nome_cpfjComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cliente", "Código" }));

        buscarjButton.setText("Buscar");
        buscarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarjButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(pesquisarjLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buscarjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nome_cpfjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buscarjButton)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buscarjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pesquisarjLabel)
                    .addComponent(nome_cpfjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buscarjButton))
                .addContainerGap())
        );

        alterarjButton.setText("Alterar");
        alterarjButton.setEnabled(false);
        alterarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarjButtonActionPerformed(evt);
            }
        });

        excluirjButton.setText("Excluir");
        excluirjButton.setEnabled(false);
        excluirjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirjButtonActionPerformed(evt);
            }
        });

        novojButton.setText("Novo");
        novojButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novojButtonActionPerformed(evt);
            }
        });

        novojButton1.setText("Atualiza");
        novojButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novojButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(alterarjButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                    .addComponent(excluirjButton, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                    .addComponent(novojButton, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                    .addComponent(novojButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(alterarjButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(excluirjButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(novojButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(novojButton1)
                .addContainerGap(101, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buscarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarjButtonActionPerformed
        conVendajTable.removeAll();

        if (nome_cpfjComboBox.getSelectedItem() == "Cliente") {
            conTableVenda con = new conTableVenda();

            conVendajTable = con.ConsultaTabelaPorCliente(conVendajTable, buscarjTextField.getText());

        } else {
            conTableVenda con = new conTableVenda();

            conVendajTable = con.ConsultaTabelaPorCodigo(conVendajTable, buscarjTextField.getText());

        }
    }//GEN-LAST:event_buscarjButtonActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        atualizaTable();
    }//GEN-LAST:event_formInternalFrameOpened

    private void novojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novojButtonActionPerformed
        this.setVisible(false);
        cadVendaJInternalFrame.getInstancia().setVisible(true);
    }//GEN-LAST:event_novojButtonActionPerformed

    private void excluirjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirjButtonActionPerformed
      if(conVendajTable.getSelectedRow()<0){
javax.swing.JOptionPane.showMessageDialog(null, "Nenhuma linha selecionada!", "Atenção", 2);}
      else{

        int i = javax.swing.JOptionPane.showConfirmDialog(null, "Esta operação é irreversível.\n Deseja realmente apagar este registro?", "EXCLUIR REGISTRO", 0, 2);

        if (i == 0) {
            dmlConta_Rec.deletar(conVendajTable.getValueAt(conVendajTable.getSelectedRow(), 1).toString());
            dmlPedido_tem_produto.deletar(conVendajTable.getValueAt(conVendajTable.getSelectedRow(), 1).toString());
            dmlPedido.deletar(conVendajTable.getValueAt(conVendajTable.getSelectedRow(), 1).toString());
            atualizaTable();
        }
      }
    }//GEN-LAST:event_excluirjButtonActionPerformed

    private void alterarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarjButtonActionPerformed
      if(conVendajTable.getSelectedRow()<0){
javax.swing.JOptionPane.showMessageDialog(null, "Nenhuma linha selecionada!", "Atenção", 2);}
      else{

        cadVendaJInternalFrame.getInstancia().limpaTudo();
        conTableVenda con = new conTableVenda();
        Pedido ped = new Pedido();
//        Contas_receber contas = new Contas_receber();

        ped = con.ConsultaPedidoPorLinha(conVendajTable.getValueAt(conVendajTable.getSelectedRow(), 1).toString());
//        contas = con.ConsultaCONTAS_RECEBERPorLinha(conVendajTable.getValueAt(conVendajTable.getSelectedRow(), 1).toString());

        cadVendaJInternalFrame.getInstancia().setObjPedido(ped);

        cadVendaJInternalFrame.getInstancia().setCliente(ped.getCPFCLIENTE(), Integer.parseInt(ped.getCLI_CDLOJA()));
        cadVendaJInternalFrame.getInstancia().setDataPGjFormattedTextField(ped.getDTPGTO());

        cadVendaJInternalFrame.getInstancia().setNFjTextField(ped.getCDNOTAFISCAL());

        Cliente cli = new conTableCliente().ConsultaObjetoPorCDCLIENTE(ped.getCPFCLIENTE());

        cadVendaJInternalFrame.getInstancia().setNomeClientejTextField(cli.getNMCLIENTE());
        cadVendaJInternalFrame.getInstancia().setEnd1jTextArea(cli.getNMENDLOGRADOURO() + ", " + Cliente.getInstancia().getNMENDNUMERO() + " - " + Cliente.getInstancia().getNMENDCOMP() + " - " + Cliente.getInstancia().getNMENDBAIRRO() + ", " + Cliente.getInstancia().getNMENDCIDADE() + " \n CEP: " + Cliente.getInstancia().getNMENDCEP());


        if (ped.getDTENTREGA() != null) {
            cadVendaJInternalFrame.getInstancia().setjCheckBox(true);
            cadVendaJInternalFrame.getInstancia().setDataEntregajFormattedTextField(ped.getDTENTREGA());
        } else {
            cadVendaJInternalFrame.getInstancia().setjCheckBox(false);
        }

        if (ped.getALT_ENDERECO() != null) {
            cadVendaJInternalFrame.getInstancia().setjRadioButton(false);
            cadVendaJInternalFrame.getInstancia().setEnd2jTextArea(ped.getALT_ENDERECO());
        } else {
            cadVendaJInternalFrame.getInstancia().setjRadioButton(true);
        }
        cadVendaJInternalFrame.getInstancia().setIcentreguejCheckBox(ped.getICENTREGUE());

        cadVendaJInternalFrame.getInstancia().setjTable1(con.ConsultaTabelaPedido_tem_Produto(cadVendaJInternalFrame.getInstancia().getjTable1(), ped.getCDVENDA()));


        //Envia tabela preenchida com produtos


cadVendaJInternalFrame.getInstancia().setAlterarVenda(true);
        cadVendaJInternalFrame.getInstancia().setVisible(true);
        this.setVisible(false);



      }
    }//GEN-LAST:event_alterarjButtonActionPerformed
public void setAlterButtons(boolean b){
    alterarjButton.setEnabled(b);
    excluirjButton.setEnabled(b);
}
    private void novojButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novojButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_novojButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterarjButton;
    private javax.swing.JButton buscarjButton;
    private javax.swing.JTextField buscarjTextField;
    private javax.swing.JTable conVendajTable;
    private javax.swing.JButton excluirjButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox nome_cpfjComboBox;
    private javax.swing.JButton novojButton;
    private javax.swing.JButton novojButton1;
    private javax.swing.JLabel pesquisarjLabel;
    // End of variables declaration//GEN-END:variables
}
